<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>客户管理</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../css/public.css" media="all">
	</head>
	<style>
		.layui-form-label {
	            width: 120px !important;
	            text-align: center !important;
	        }
	 
	        .layui-input-block {
	            margin-left: 120px !important;
	        }
	    </style>
	<body>
		<div class="layuimini-container">
			<div class="layuimini-main">
				<fieldset class="layui-elem-field layuimini-search">
					<legend>搜索信息</legend>
					<div style="margin: 10px 10px 10px 10px">
						<form class="layui-form layui-form-pane" action="" id="searchFrm">
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">客户全称</label>
									<div class="layui-input-inline">
										<input type="text" name="customername" autocomplete="off" class="layui-input" placeholder="请输入客户全称">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">联系人</label>
									<div class="layui-input-inline">
										<input type="text" name="connectionpersion" autocomplete="off" class="layui-input" placeholder="请输入联系人">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">联系电话</label>
									<div class="layui-input-inline">
										<input type="text" name="phone" autocomplete="off" class="layui-input" placeholder="请输入联系电话">
									</div>
								</div>
								<div class="layui-inline">
									<div class="layui-block" style="text-align: center;">
										<a class="layui-btn" lay-submit="" lay-filter="data-search-btn">
											<i class="layui-icon layui-icon-search"></i>搜索</a>
										<a class="layui-btn layui-btn-warm" lay-submit="" lay-filter="data-reset-btn">
											<i class="layui-icon layui-icon-refresh"></i>重置</a>
									</div>
								</div>
							</div>
						</form>
					</div>
				</fieldset>
				<!--表格开始-->
				<table class="layui-hide" id="customerTable" lay-filter="customerFilter"></table>
				<script type="text/html" id="availableTpl">
					<input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用" lay-filter="availableSwitch" {{ d.available == 1 ? 'checked' : '' }}>
				</script>
				<div style="display: none;" id="customerToolBar">
					<a class="layui-btn layui-btn-sm btn-add btn-add-customer" lay-event="addCustomer">
						<i class="layui-icon layui-icon-addition"></i>添加客户</a>
					<a class="layui-btn layui-btn-sm layui-btn-danger btn-batchdelete btn-batchdelete-customer" lay-event="batchdelete">
					<i class="layui-icon layui-icon-delete"></i>批量删除</a>
				</div>
				<div style="display: none;" id="customerBar">
					<a class="layui-btn layui-btn-xs btn-update" lay-event="update">编辑</a>
					<a class="layui-btn layui-btn-xs layui-btn-danger btn-delete btn-delete-customer" lay-event="delete">删除</a>
				</div>
				<!--表格结束-->

				<!--添加和修改的弹层开始-->
				<div id="addOrUpdateDiv" style="display: none; padding: 0.3125rem;">
					<form method="post" class="layui-form layui-form-pane" lay-filter="dataFilter" id="dataFrm">					
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">客户全称</label>
								<div class="layui-input-block">
									<input type="hidden" name="id" />
									<input type="text" name="customername" autocomplete="off" placeholder="请输入客户全称" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">客户邮编</label>
								<div class="layui-input-block">
									<input type="text" name="zip" autocomplete="off" placeholder="请输入客户邮编" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">客户公司电话</label>
								<div class="layui-input-block">
									<input type="text" name="telephone" autocomplete="off" placeholder="请输入客户公司电话" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">客户传真</label>
								<div class="layui-input-block">
									<input type="text" name="fax" autocomplete="off" placeholder="请输入客户传真" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">客户公司地址</label>
								<div class="layui-input-block">
									<input type="text" name="address" autocomplete="off" placeholder="请输入客户公司地址" class="layui-input" lay-verify="required">
								</div>
							</div>
						</div>

						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">联系人</label>
								<div class="layui-input-block">
									<input type="text" name="connectionpersion" autocomplete="off" placeholder="请输入联系人" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">联系人电话</label>
								<div class="layui-input-block">
									<input type="text" name="phone" autocomplete="off" placeholder="请输入联系电话" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">联系人信箱</label>
								<div class="layui-input-block">
									<input type="text" name="email" autocomplete="off" placeholder="请输入联系人信箱" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">卡户银行</label>
								<div class="layui-input-block">
									<input type="text" name="bank" autocomplete="off" placeholder="请输入卡户银行" class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">银行卡账号</label>
								<div class="layui-input-block">
									<input type="text" name="account" autocomplete="off" placeholder="请输入银行账号" class="layui-input" lay-verify="required">
								</div>
							</div>
						</div>
						<div class=class="layui-form-item" style="text-align: center;">
							<a class="layui-btn" lay-submit="" lay-filter="doSubmit">
								<i class="layui-icon layui-icon-release"></i>立即提交</a>
						</div>
					</form>
				</div>
				<!--添加和修改的弹层结束-->
			</div>
		</div>

		<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
		<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
		<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
		<script src="../../../lib/common/common.js" charset="utf-8"></script>

		<script>
			layui.use(['form', 'table', 'layer'], function() {
				var $ = layui.jquery,
					form = layui.form,
					table = layui.table,
					layer = layui.layer; // 弹出层


				// 加载 数据
				var tableIns = table.render({
					elem: '#customerTable',
					url: api + 'customer/loadAllCustomer',
					title: '菜单数据表',
					toolbar: '#customerToolBar',
					height: 'full-220',

					cols: [
						[{
								type: "checkbox",
								fixed: "left"
							},
							{
								field: 'customername',
								title: '客户全称',
								align: "center",
								width: 120
							},
							{
								field: 'zip',
								title: '客户邮编',
								align: "center",
								width: 90
							},
							{
								field: 'address',
								title: '客户公司地址',
								align: "center",
								width: 120
							},
							{
								field: 'telephone',
								title: '客户公司电话',
								align: "center",
								width: 120
							},
							{
								field: 'fax',
								title: '客户传真',
								align: "center",
								width: 120
							},
							{
								field: 'connectionpersion',
								title: '联系人',
								align: "center",
								width: 80
							},
							{
								field: 'phone',
								title: '联系人电话',
								align: "center",
								width: 120
							},
							{
								field: 'email',
								title: '联系人信箱',
								align: "center",
								width: 200
							},
							{
								field: 'bank',
								title: '开户银行',
								align: "center",
								width: 120
							},
							{
								field: 'account',
								title: '银行卡账号',
								align: "center",
								width: 200
							},
							
							{
								field: 'available ',
								title: '是否可用',
								align: "center",
								width: 120,
								templet: '#availableTpl'
							},

							{
								title: '操作',
								templet: '#customerBar',
								fixed: "right",
								align: "center",
								width: 120
							}
						]
					],
					done: function(data, curr, count) {
						//不是第一页时，如果当前返回的数据为0那么就返回上一页
						if (data.data.length === 0 && curr !== 1) {
							tableIns.reload({
								page: {
									curr: curr - 1
								}
							})
						}
					},
					// limits: [10, 15, 20, 25, 50, 100],
					// limit: 10,
					page: true
				});

				// 监听搜索操作
				form.on('submit(data-search-btn)', function(data) {
					var params = $("#searchFrm").serialize();

					//执行搜索重载
					table.reload('customerTable', {
						page: {
							curr: 1
						},
						url: api + 'customer/loadAllCustomer?' + params
					});
					return false;
				});

				// 监听重置操作
				form.on('submit(data-reset-btn)', function(data) {
					$("#searchFrm")[0].reset(); // 清空搜索信息框中的数据
					//执行搜索重载
					table.reload('customerTable', {
						page: {
							curr: 1
						},
						url: api + 'customer/loadAllCustomer'
					});
					return false;
				});
				
				form.on('submit(dataFrm-reset-btn)', function(data) {
					$("#dataFrm")[0].reset(); // 清空表格信息框中的数据
					$("#addOrUpdateDiv").val("");
					return false;
				});

				// 监听表头的事件
				table.on('toolbar(customerFilter)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'batchdelete':
							batchdelete();
							break;
						case 'addCustomer':
							addCustomer();
							break;
					};
				});

				// 监听行数据的事件
				table.on('tool(customerFilter)', function(obj) {
					var data = obj.data; // 获得当前行数据
					if (obj.event === 'delete') {
						layer.confirm('真的删除行么？', {
							icon: 3,
							title: '提示'
						}, function(index) {
							$.post(api + 'customer/deleteCustomer', {
									id: data.id
								},
								function(res) {
									if (res.code == 200) {
										tableIns.reload();
									}
									layer.msg(res.msg);
								})
							layer.close(index);
						});
					} else if(obj.event == 'update') {
						updateCustomer(data);
					}
				});

				/**
				 * 监听按钮
				 */
				form.on('switch(availableSwitch)', function(obj) {
					$.post(api + 'customer/updateCustomer', {
						id: this.value,
						available: obj.elem.checked ? 1 : 0
					}, function(res) {
						// layer.msg(res.msg)
					})
				});

				/**
				 * 批量删除
				 */
				function batchdelete() {
					var checkStatus = table.checkStatus('customerTable'); //customerTable 即为基础参数 id 对应的值
					var data = checkStatus.data; //获取选中行的数据
					var length = checkStatus.data.length; //获取选中行数量，可作为是否有选中行的条件

					if (length == 0) {
						layer.msg('请选择要删除的行数据');
					} else {
						layer.confirm('真的要删除选中的这些数据么?', {
							icon: 3,
							title: '提示'
						}, function(index) {
							var ids = "";
							$.each(data, function(index, item) {
								if (index == 0) {
									ids += "ids=" + item.id;
								} else {
									ids += "&ids=" + item.id;
								}
							});
							$.post(api + 'customer/batchDeleteCustomer', ids,
								function(res) {
									if (res.code == 200) {
										tableIns.reload();
									}
									layer.msg(res.msg);
								})
							layer.close(index);
						});
					}
				}
				

				/**
				 * 打开添加客户弹出层
				 */
				var mainIndex;
				var url; // 动态的url
				function addCustomer() {
					mainIndex = layer.open({
						type: 1,
						title: '添加客户',
						area: ['650px'],
						content: $("#addOrUpdateDiv"),
						success: function(index) {
							url = api + "customer/addCustomer";
							$("#dataFrm")[0].reset();
						}
					})
				}
				/**
				 * 打开客户管理的编辑弹出层
				 */
				function updateCustomer(data){
					mainIndex = layer.open({
						type: 1,
						title: '修改客户信息',
						area: ['650px'],
						content: $("#addOrUpdateDiv"),
						success: function(index) {		
							url = api + "customer/updateCustomer";
							// 装载数据表格 	
							form.val("dataFilter", data);
						}
					})
				}

				/**
				 * 提交数据
				 */
				form.on('submit(doSubmit)', function(data) {
					var params = $("#dataFrm").serialize();
					$.post(url, params, function(res) {
						layer.msg(res.msg);
						if (res.code == 200) {
							tableIns.reload();
						}
						layer.close(mainIndex);
					})
					return false;
				});

			});
		</script>
		<script>

		</script>

	</body>
</html>
